package com.lineage.data.item_etcitem.add;

import java.util.Random;

import com.lineage.config.Configtype;
import com.lineage.data.cmd.EnchantWeapon;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;

public class Dai100_ScrollEnchantWeapon extends ItemExecutor {
  public static ItemExecutor get() {
    return new Dai100_ScrollEnchantWeapon();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int targObjId = data[0];
    L1ItemInstance tgItem = pc.getInventory().getItem(targObjId);
    if (tgItem == null)
      return; 
    int safe_enchant = tgItem.getItem().get_safeenchant();
    boolean isErr = false;
    int use_type = tgItem.getItem().getUseType();
    switch (use_type) {
      case 1:
        if (safe_enchant < 0)
          isErr = true; 
        break;
      default:
        isErr = true;
        break;
    } 
    int weaponId = tgItem.getItem().getItemId();
    if (weaponId >= 246 && weaponId <= 255)
      isErr = true; 
    if (tgItem.getBless() >= 128)
      isErr = true; 
    if (isErr) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      return;
    } 
	if (tgItem.getEnchantLevel() < Configtype.weaponlv) {
		pc.getInventory().removeItem(item, 1L);
		EnchantWeapon enchantWeapon = new EnchantWeapon();
		int randomELevel = enchantWeapon.randomELevel(tgItem, item.getBless());
//		if (randomELevel == 0) {
//			Random random = new Random();
//			int i = random.nextInt(100) + 1;
//			if (i <= 20) {
//				enchantWeapon.successEnchant(pc, tgItem, randomELevel);
//			} else {
//				enchantWeapon.failureEnchant(pc, tgItem);
//			}
//		}
		enchantWeapon.successEnchant(pc, tgItem, randomELevel);
	} else {
		pc.sendPackets((ServerBasePacket) new S_ServerMessage("已达到武器最高强化阶段。"));
	}
}
}
